package com.tarzan.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tarzan.api.model.request.role.SysRoleIdReq;
import com.tarzan.api.model.request.role.UserBindingRoleReq;
import com.tarzan.api.model.request.user.RoleBindUserReq;
import com.tarzan.api.model.request.user.SysUserIdReq;
import com.tarzan.api.model.response.role.SysRoleVo;
import com.tarzan.api.model.response.user.UserRoleResp;
import com.tarzan.common.core.base.BaseResponse;
import com.tarzan.common.core.exception.BusinessException;
import com.tarzan.system.service.ISysUserRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liujinshan
 * @since 2020-05-19
 */
@RestController
@RequestMapping("userRole")
@CrossOrigin  //解决跨域
public class SysUserRoleController {
    private final static Logger logger = LoggerFactory.getLogger(SysUserRoleController.class);

    @Autowired
    private ISysUserRoleService iSysUserRoleService;

    /**
     * 根据用户ID查询对应的角色信息
     * @param sysUserIdReq
     * @return
     */
    @PostMapping(value = "findRoleInfoByUserId",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse<List<SysRoleVo>> findRoleInfoByUserId(@RequestBody SysUserIdReq sysUserIdReq){
        BaseResponse<List<SysRoleVo>> response = new BaseResponse<>();
        try {
            response = BaseResponse.ok(iSysUserRoleService.findRoleInfoByUserId(sysUserIdReq));
        } catch (BusinessException e) {
            response.setErrCode(e.getCode());
            response.setErrMsg(e.getMsg());
            logger.info("SysUserRoleController -> findRoleInfoByUserId() BusinessException ex="+e);
        } catch (Exception e) {
            response.setErrCode("999");
            response.setErrMsg(e.getMessage());
            logger.info("SysUserRoleController -> findRoleInfoByUserId() Exception e="+e);
        }
        return response;
    }

    /**
     * 角色绑定用户
     * @return
     */
    @PostMapping(value = "roleBindUser",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse roleBindUser(@RequestBody RoleBindUserReq req){
        try {
            iSysUserRoleService.roleBindUser(req);
        } catch (BusinessException e) {
            logger.info("SysUserRoleController -> roleBindUser() BusinessException ex="+e);
            return BaseResponse.error(e.getCode(), e.getMsg());
        } catch (Exception e) {
            logger.info("SysUserRoleController -> roleBindUser() Exception e="+e);
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }

    /**
     * 根据角色id查询对应的用户信息
     * @param roleIdReq
     * @return
     */
    @PostMapping(value = "getPageUserRoleInfoByRoleId",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse<IPage<UserRoleResp>> getPageUserRoleInfoByRoleId(@RequestBody SysRoleIdReq roleIdReq){
        BaseResponse<IPage<UserRoleResp>> baseResponse = new BaseResponse<>();
        try {
            baseResponse = BaseResponse.ok(iSysUserRoleService.getPageUserRoleInfoByRoleId(roleIdReq));
        } catch (BusinessException e) {
            baseResponse.setErrCode(e.getCode());
            baseResponse.setErrMsg(e.getMsg());
            logger.info("SysUserRoleController -> getPageUserRoleInfoByRoleId() BusinessException ex="+e);
        } catch (Exception e) {
            baseResponse.setErrCode("999");
            baseResponse.setErrMsg(e.getMessage());
            logger.info("SysUserRoleController -> getPageUserRoleInfoByRoleId() Exception e="+e);
        }
        return baseResponse;
    }

    /**
     * 用户绑定角色
     * @param userBindingRoleReq
     * @return
     */
    @PostMapping(value = "insertUserBindingRole",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse insertUserBindingRole(@RequestBody UserBindingRoleReq userBindingRoleReq){
        try {
            iSysUserRoleService.insertUserBindingRole(userBindingRoleReq);
        } catch (BusinessException e) {
            logger.info("SysUserRoleController -> insertUserBindingRole() BusinessException ex="+e);
            return BaseResponse.error(e.getCode(), e.getMsg());
        } catch (Exception e) {
            logger.info("SysUserRoleController -> insertUserBindingRole() Exception e="+e);
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }
}
